<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生注册</title>
    <script src="js/base.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            /* 背景图设置：覆盖全屏、居中显示、固定不滚动 */
            background-image: url("images/beijingtu.jpg"); /* 需替换为实际背景图路径 */
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            background-repeat: no-repeat;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            /* 半透明黑色叠加层，提升文字可读性 */
            position: relative;
        }

        /* 背景图叠加层 */
        body::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5); /* 黑色半透明，可调整透明度 */
            z-index: -1; /* 确保叠加层在背景图之上、内容之下 */
        }

        .container {
            background-color: rgba(255, 255, 255, 0.95); /* 表单容器半透明白色，减少背景干扰 */
            width: 100%;
            max-width: 500px;
            padding: 15px 30px;
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
            z-index: 1; /* 确保表单在叠加层之上 */
        }
        h1 {
            color: #1a73e8;
            text-align: center;
            margin-bottom: 30px;
            font-weight: 600;
            font-size: 28px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #5f6368;
            font-size: 14px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #dadce0;
            border-radius: 8px;
            font-size: 16px;
            transition: all 0.2s ease;
            background-color: white; /* 输入框纯白背景，提升输入清晰度 */
        }

        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: #1a73e8;
            box-shadow: 0 0 0 2px rgba(26, 115, 232, 0.2);
        }

        .gender-group {
            display: flex;
            align-items: center;
            gap: 20px;
            margin: 10px 0;
        }

        input[type="radio"] {
            margin-right: 8px;
            accent-color: #1a73e8;
        }

        .radio-label {
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            color: #3c4043;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #1a73e8;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.2s ease;
            margin-top: 10px;
        }

        button:hover {
            background-color: #0d66d0;
        }

        #usernameAlert, #passwordAlert, #userSidAlert {
            font-size: 13px;
            margin-top: 5px;
            display: block;
            height: 16px;
        }

        .link-text {
            text-align: center;
            margin-top: 20px;
            color: #5f6368;
            font-size: 14px;
        }

        .link-text a {
            color: #1a73e8;
            text-decoration: none;
            font-weight: 500;
        }

        .link-text a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>学生注册</h1>
    <div class="form-group">
        <label for="username">用户名</label>
        <input type="text" id="username" onchange="onUsernameChanged()" placeholder="请输入用户名"/>
        <span id="usernameAlert"></span>
    </div>

    <div class="form-group">
        <label for="password">密码</label>
        <input type="password" id="password" onchange="onPasswordChanged();" placeholder="请输入6-16位密码"/>
    </div>

    <div class="form-group">
        <label for="rePassword">再次输入密码</label>
        <input type="password" id="rePassword" onchange="onPasswordChanged();" placeholder="请再次输入密码"/>
        <span id="passwordAlert"></span>
    </div>

    <div class="form-group">
        <label>性别</label>
        <div class="gender-group">
            <label class="radio-label">
                <input type="radio" name="gender" id="nan" value="男" checked="checked"/>男
            </label>
            <label class="radio-label">
                <input type="radio" name="gender" id="nv" value="女"/>女
            </label>
        </div>
    </div>

    <button onclick="onSignUpClick()">注册</button>

    <div class="link-text">
        已有账号？<a href="signIn.html">立即登录</a>
    </div>
</div>

<script>
        let usernameFlag = false;
        let passwordFlag = false;
        function onUsernameChanged() {
            let username = document.getElementById("username").value;
            $.rest("http://localhost:8080/user/findUsername?username=" + username, "get", null, function (r) {
                const alertEl = document.getElementById("usernameAlert");
                if (r.code == 200) {
                    alertEl.style.color = "green";
                    alertEl.innerHTML = "用户名可用";
                    usernameFlag = true;
                } else {
                    alertEl.style.color = "red";
                    alertEl.innerHTML = "用户名不可用";
                    usernameFlag = false;
                }
            });
        }

        function onPasswordChanged() {
            let password = document.getElementById("password").value;
            let rePassword = document.getElementById("rePassword").value;
            const alertEl = document.getElementById("passwordAlert");

            if (!password) {
                alertEl.style.color = "orange";
                alertEl.innerHTML = "请输入密码";
                passwordFlag = false;
                return;
            }

            if (password.length < 6 || password.length > 16) {
                alertEl.style.color = "red";
                alertEl.innerHTML = "密码长度需为6-16位";
                passwordFlag = false;
                return;
            }

            if (password != rePassword) {
                alertEl.style.color = "red";
                alertEl.innerHTML = "两次密码输入不一致";
                passwordFlag = false;
            } else {
                alertEl.style.color = "green";
                alertEl.innerHTML = "两次密码输入一致";
                passwordFlag = true;
            }
        }

        function onSignUpClick() {
            if (usernameFlag && passwordFlag && sidFlag) {
                let username = document.getElementById("username").value;
                let password = document.getElementById("password").value;
                let gender = document.getElementById("nan").checked ? "男" : "女";

                let json = {
                    username: username,
                    password: password,
                    gender: gender,
                };

                $.rest("http://localhost:8080/user/signUp", "Post", json, function (r) {
                    if (r.code == 200) {
                        alert("注册成功");
                        window.location.href = "signIn.html";
                    } else {
                        alert("注册失败");
                    }
                });
            } else {
                alert("请检查输入信息是否正确");
            }
        }
    </script>
</body>
</html>